﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace Hongsh.Pay.Client.Proxy.Ssl
{
	// Token: 0x02000020 RID: 32
	public static class ClientHelloExtends
	{
		// Token: 0x060000E1 RID: 225 RVA: 0x0000AA9C File Offset: 0x00008C9C
		public static int ReadInt16BigEndian(this Stream s)
		{
			byte[] array = new byte[]
			{
				0,
				(byte)s.ReadByte()
			};
			array[0] = (byte)s.ReadByte();
			return BitConverter.ToInt16(array, 0);
		}

		// Token: 0x060000E2 RID: 226 RVA: 0x0000AAD0 File Offset: 0x00008CD0
		public static int ReadInt24BigEndian(this Stream s)
		{
			byte[] array = new byte[4];
			array[2] = (byte)s.ReadByte();
			array[1] = (byte)s.ReadByte();
			array[0] = (byte)s.ReadByte();
			return BitConverter.ToInt16(array, 0);
		}

		// Token: 0x060000E3 RID: 227 RVA: 0x0000AB0C File Offset: 0x00008D0C
		public static byte[] ReadBytes(this Stream s, int len)
		{
			byte[] array = new byte[len];
			s.Read(array, 0, array.Length);
			return array;
		}

		// Token: 0x060000E4 RID: 228 RVA: 0x0000AB30 File Offset: 0x00008D30
		public static SslClientHello PeekClientHello(this Socket socket)
		{
			byte[] array = new byte[4096];
			MemoryStream memoryStream = new MemoryStream(array, 0, socket.Receive(array, SocketFlags.Peek));
			Encoding.UTF8.GetString(array, 0, (int)memoryStream.Length);
			int num = memoryStream.ReadByte();
			SslClientHello result;
			if (num == -1)
			{
				result = null;
			}
			else if ((num & 128) == 128)
			{
				int num2 = ((num & 127) << 8) + memoryStream.ReadByte();
				if (num2 < 9)
				{
					result = null;
				}
				else if (memoryStream.ReadByte() != 1)
				{
					result = null;
				}
				else
				{
					int majorVersion = memoryStream.ReadByte();
					int minorVersion = memoryStream.ReadByte();
					int num3 = memoryStream.ReadInt16BigEndian() / 3;
					int len = memoryStream.ReadInt16BigEndian();
					int len2 = memoryStream.ReadInt16BigEndian();
					int[] array2 = new int[num3];
					for (int i = 0; i < array2.Length; i++)
					{
						array2[i] = (memoryStream.ReadByte() << 16) + (memoryStream.ReadByte() << 8) + memoryStream.ReadByte();
					}
					byte[] sessionId = memoryStream.ReadBytes(len);
					byte[] random = memoryStream.ReadBytes(len2);
					result = new SslClientHello
					{
						HandshakeVersion = 2,
						MajorVersion = majorVersion,
						MinorVersion = minorVersion,
						Random = random,
						SessionId = sessionId,
						Ciphers = array2,
						ClientHelloLength = (int)memoryStream.Position
					};
				}
			}
			else if (num == 22)
			{
				if (memoryStream.Length - memoryStream.Position < 43L)
				{
					result = null;
				}
				else
				{
					int num4 = memoryStream.ReadByte();
					int num5 = memoryStream.ReadByte();
					int num6 = memoryStream.ReadInt16BigEndian();
					if (memoryStream.ReadByte() != 1)
					{
						result = null;
					}
					else
					{
						int num7 = memoryStream.ReadInt24BigEndian();
						num4 = memoryStream.ReadByte();
						num5 = memoryStream.ReadByte();
						byte[] random2 = memoryStream.ReadBytes(32);
						num7 = memoryStream.ReadByte();
						if (memoryStream.Length - memoryStream.Position < num7 + 2)
						{
							result = null;
						}
						else
						{
							byte[] sessionId2 = memoryStream.ReadBytes(num7);
							num7 = memoryStream.ReadInt16BigEndian();
							if (memoryStream.Length - memoryStream.Position < num7 + 1)
							{
								result = null;
							}
							else
							{
								byte[] array3 = memoryStream.ReadBytes(num7);
								int[] array4 = new int[array3.Length / 2];
								for (int j = 0; j < array4.Length; j++)
								{
									array4[j] = (array3[2 * j] << 8) + array3[2 * j + 1];
								}
								num7 = memoryStream.ReadByte();
								if (num7 < 1)
								{
									result = null;
								}
								else if (memoryStream.Length - memoryStream.Position < num7)
								{
									result = null;
								}
								else
								{
									byte[] compressionData = memoryStream.ReadBytes(num7);
									int num8 = (int)memoryStream.Position;
									Dictionary<string, SslExtension> dictionary = null;
									if (num8 < num6 + 5 && (num4 > 3 || (num4 == 3 && num5 >= 1)) && memoryStream.Length - memoryStream.Position > 2L)
									{
										int k = memoryStream.ReadInt16BigEndian();
										if (memoryStream.Length - memoryStream.Position >= k)
										{
											dictionary = new Dictionary<string, SslExtension>();
											int num9 = 0;
											while (k > 3)
											{
												int value = memoryStream.ReadInt16BigEndian();
												num7 = memoryStream.ReadInt16BigEndian();
												byte[] data = memoryStream.ReadBytes(num7);
												SslExtension extension = SslExtension.GetExtension(value, data, num9++);
												dictionary[extension.Name] = extension;
												k -= 4 + num7;
											}
										}
									}
									SslClientHello sslClientHello = new SslClientHello
									{
										HandshakeVersion = 3,
										MajorVersion = num4,
										MinorVersion = num5,
										Random = random2,
										SessionId = sessionId2,
										Ciphers = array4,
										CompressionData = compressionData,
										ClientHelloLength = (int)memoryStream.Position,
										EntensionsStartPosition = num8,
										Extensions = dictionary
									};
									result = sslClientHello;
								}
							}
						}
					}
				}
			}
			else
			{
				result = null;
			}
			return result;
		}
	}
}
